<template>
<div class="YDynamicTags">
  <el-tag
      :key="tag"
      v-for="tag in value"
      :closable="!disabled"
      :disable-transitions="false"
      @close="handleClose(tag)">
    {{tag}}
  </el-tag>
  <template v-if="!disabled">
    <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput">{{tips}}</el-button>
  </template>
</div>
</template>

<script>
export default {
  name: "YDynamicTags",
  props:{
    value: Array,
    tips: {
      type: String,
      default: '+ New Tag'
    },
    disabled: Boolean
  },
  data() {
    return {
      inputVisible: false,
      inputValue: ''
    };
  },
  methods: {
    handleClose(tag) {
      this.value.splice(this.value.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.value.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>

<style scoped lang="less">
.YDynamicTags{
  display: inline-block;
  /deep/ .el-tag + .el-tag {
    margin-left: 10px;
  }
  /deep/ .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  /deep/ .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>